package com.examples.ry.layouts;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class InstallerWizard {

	public static void main(String[] args) {
		final Display display = new Display();
		int style = SWT.SHELL_TRIM;
		// int style = SWT.NONE; //This will remove the title bar to give more installer like look.
		final Shell shell = new Shell(display, style);
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		shell.setLayout(layout);
		screen1(shell);

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	static private void screen1(final Shell shell) {
		shell.setText("Screen 1");
		Control childControl[] = shell.getChildren();
		for (Control child : childControl) {
			child.dispose();
		}
		final Label label = new Label(shell, SWT.BORDER);
		label.setText("Test Wizard");
		final Text text = new Text(shell, SWT.BORDER | SWT.SINGLE);

		final Button button = new Button(shell, SWT.PUSH);
		button.setText("Next");
		button.pack();
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				screen2(shell);
			}
		});

		final Button cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.pack();
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				shell.getDisplay().dispose();
			}
		});

	}

	static private void screen2(final Shell shell) {

		Control[] controls = shell.getChildren();

		Rectangle rect = controls[0].getBounds();
		Rectangle rect1 = controls[1].getBounds();
		Rectangle rect2 = controls[2].getBounds();
		Rectangle rect3 = controls[3].getBounds();
		shell.setText("Screen 2");
		Control childControl[] = shell.getChildren();
		for (Control child : childControl) {
			child.dispose();
		}

		final Label label = new Label(shell, SWT.BORDER);
		label.setText("Screen 2");
		label.setBounds(rect);
		label.pack();

		final Text text = new Text(shell, SWT.BORDER | SWT.SINGLE);
		text.pack();
		text.setBounds(rect1);

		final Button backButton = new Button(shell, SWT.PUSH);
		backButton.setText("Back");
		backButton.setBounds(rect3.x + 100,rect3.y,rect3.height,rect3.width);
		backButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				screen1(shell);
			}
		});
		backButton.pack();


		final Button button = new Button(shell, SWT.PUSH);
		button.setText("Next");
		button.setBounds(rect2);
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				screen3(shell);
			}
		});
		button.pack();

		final Button cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.pack();
		cancelButton.setBounds(rect3);
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				shell.getDisplay().dispose();
			}
		});
		shell.redraw();
	}

	static private void screen3(final Shell shell) {
		shell.setText("Screen 3");
		Control childControl[] = shell.getChildren();
		for (Control child : childControl) {
			child.dispose();
		}

		final Label label = new Label(shell, SWT.BORDER);
		label.setText("Thanks");
		label.pack();

		final Button cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setText("Cancel");
		cancelButton.pack();
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				// Cleanup and dispose
				shell.getDisplay().dispose();
			}
		});

		final Button backButton = new Button(shell, SWT.PUSH);
		backButton.setText("Back");
		backButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				screen2(shell);
			}
		});
		backButton.pack();

		final Button finishButton = new Button(shell, SWT.PUSH);
		finishButton.setText("Finish");
		finishButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {

				shell.getDisplay().dispose();
			}
		});
		finishButton.pack();
		shell.redraw();
	}
}
